//
//  testdominion.c
//  
//
//  Created by Preston Skaggs on 2/20/14.
//  There are several issues with this tester.
//  Using the same seed number produces the same outcome every time
//  Playing Feast causes an infinite loop

#include "tdInterface.h"
#include <stdio.h>
#include <time.h>

#define MIN_PLAYERS 2
#define TIMEOUT 2
#define NUM_TESTS 100
/**************************************
 Supporting Functions
 *************************************/

int main(int argc, char* argv[]) {
		//Array to hold bot presence
	int isBot[MAX_PLAYERS] = { 0, 0, 0, 0};
    
	int players[MAX_PLAYERS];
	int playerNum;
	int outcome;
	int currentPlayer;
	int gameOver = FALSE;
	int gameStarted = FALSE;
	int turnNum = 0;
	int randomSeed;
	int kCards[NUM_K_CARDS];
    int numPlayers;
    int score[MAX_PLAYERS];
   // FILE *fd;
    struct gameState *game = malloc(sizeof(struct gameState));
    randomSeed = atoi(argv[1]);
    //fd = fopen("./randomTest.out", "w");
    memset(game,0,sizeof(struct gameState));
    
    if(argc != 2){
        printf("Usage: testdominion [integer random number seed > 0]\n");
        return EXIT_SUCCESS;
    }
    
    if(randomSeed <= 0){
        printf("Usage: testdominion [integer random number seed > 0]\n");
        return EXIT_SUCCESS;
    }
    
    //randomSeed = testNum+1;
    numPlayers = floor(Random() * (MAX_PLAYERS - MIN_PLAYERS))+MIN_PLAYERS;
    for(playerNum = 0; playerNum < numPlayers; playerNum++) {
        isBot[playerNum] = TRUE;
    }
    selectKingdomCards(randomSeed, kCards);
    outcome = initializeGame(numPlayers, kCards, randomSeed, game);
    printf("\n");
    if(outcome == SUCCESS){
        gameStarted = TRUE;
        currentPlayer = whoseTurn(game);
        printf("Game initilized with %d players\n", numPlayers);
        printf("Player %d's turn number %d\n\n", currentPlayer, turnNum);
    }
    
    
    while(TRUE) {
        
        gameOver = isGameOver(game);
        if(gameStarted == TRUE && gameOver == TRUE){
            printScores(game);
            getWinners(players, game);
            printf("After %d turns, the winner(s) are:\n", turnNum);
            for(playerNum = 0; playerNum < game->numPlayers; playerNum++){
                if(players[playerNum] == WINNER) printf("Player %d\n", playerNum);
            }
            for(playerNum = 0; playerNum < numPlayers; playerNum++) {
                score[playerNum] = scoreFor(playerNum,game);
                printf("Player %d has a score of %d\n", playerNum, score[playerNum]);
            }
            for(playerNum = 0; playerNum < game->numPlayers; playerNum++){
                printHand(playerNum, game);
                printPlayed(playerNum, game);
                printDiscard(playerNum, game);
                printDeck(playerNum, game);
            }
            
            break; //Exit out of the game/while loop
        }
        
        
        if(isBot[currentPlayer] == TRUE) {
            executeBotTurn(currentPlayer, &turnNum, game, kCards);
            currentPlayer = game->whoseTurn;
            continue;
        }
        
    }
    return EXIT_SUCCESS;
    
}
